package com.astrob.adapters;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.HistoryData;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.nhcmap.R;

public class MidDestAdapter extends BaseAdapter {
	static class ViewHolder{
		TextView text;
		ImageButton action;
	}
	boolean hasMid = false;
	public void setHasMid(boolean hasMid){
		this.hasMid = hasMid;
	}
	Context mContext = null;
	public MidDestAdapter(Context context){
		mContext = context;
	}
	@Override
	public int getCount() {
		if(!hasMid){
			return 2;
		}
		return 3;
	}
	private int getDestCount(){
		return RoutePlanDataHandle.getInstance().getDests().size();
	}
	@Override
	public Object getItem(int position) {
		if(position == 0){
			return RoutePlanDataHandle.getInstance().getStart();
		} else if(position <= getDestCount()){
			return RoutePlanDataHandle.getInstance().getDests().get(position-1);
		}
		return new HistoryData();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public interface MidSwitchDestListener{
		//当只有起点时，不能切换
		public void onSwitch();
	}
	MidSwitchDestListener mListener = null;
	public void setListener(MidSwitchDestListener listener){
		mListener = listener;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext,
					R.layout.adapter_routeplan_middest, null);

			holder = new ViewHolder();
			holder.action = (ImageButton)convertView.findViewById(R.id.route_dest_action);
			holder.text = (TextView) convertView.findViewById(R.id.route_dest_text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		HistoryData data = (HistoryData) getItem(position);
		if(position == 0){
			holder.text.setTextColor(0xff3b90df);
			holder.text.setText(data.getName());
			holder.action.setVisibility(View.VISIBLE);
			holder.action.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mListener != null){
						mListener.onSwitch();
					}
				}
			});
		}
		else if(position == 1){
			holder.text.setTextColor(0xff303030);
			if(hasMid){
				holder.text.setHint("请输入中途点");
			}
			else {
				holder.text.setHint("请输入终点");
			}
			holder.text.setText(data.getName());
			holder.action.setVisibility(View.GONE);
		} else {
			holder.text.setTextColor(0xff303030);
			holder.text.setHint("请输入终点");
			holder.text.setText(data.getName());
			holder.action.setVisibility(View.GONE);
		}
		return convertView;
	}

}
